package com.hxtec.polaris.commons.tasks;

import com.hxtec.polaris.commons.exception.MyException;
import com.hxtec.polaris.mapper.ShopLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Describtion 操作记录任务
 * @Author yonyong
 * @Date 2020/1/3 9:28
 * @Version 1.0.0
 **/
@Component
@Transactional
public class RecordsTask {

    private static final Logger logger = LoggerFactory.getLogger(RecordsTask.class);

    @Resource
    ShopLogMapper tblLogMapper;

    /**
     * 超过十万条自动清除
     */
    @Scheduled(cron = "${mytask.day}")
    public void autoClearRecords(){
        logger.info("操作记录任务开始");
        try{
            int count = tblLogMapper.selectCount(null);
            if (count > 100000){
                int total = tblLogMapper.deleteOverRows(100000);
                logger.info("操作记录任务--删除数据{0}条",total);
            }
        }catch (Exception e){
            throw new MyException("清除操作记录被阻塞");
        }
        logger.info("操作记录任务结束");
    }
}
